package com.athuiju.eduservice.controller.front;

import com.athuiju.commonutils.CourseWebVoOrder;
import com.athuiju.commonutils.R;
import com.athuiju.eduservice.entity.EduCourse;
import com.athuiju.eduservice.entity.frontVo.CourseQueryVo;
import com.athuiju.eduservice.entity.frontVo.CourseWebVo;
import com.athuiju.eduservice.entity.vo.ChapterVo;
import com.athuiju.eduservice.service.EduChapterService;
import com.athuiju.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("eduservice/coursefront")

public class CourseFrontController {
@Autowired
    EduCourseService courseService;
@Autowired
    private EduChapterService chapterService;
    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){

        //创建Page对象
        Page<EduCourse> pageParam =new Page<EduCourse>(page,limit);
        Map<String,Object> map=courseService.pageListWeb(pageParam,courseQuery);

        return  R.ok().data(map);

    }
    @GetMapping("getFrontCourseInfo/{courseId}")
    public  R getFrontCourseInfo(@PathVariable String courseId){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo>chapterVoList=chapterService.getChapterVideoByCourseId(courseId);
        return  R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo couresInfo=courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(couresInfo,courseWebVoOrder);
        return  courseWebVoOrder;
    }

}
